<?php

namespace addons\shopro\controller\teams;

use addons\shopro\model\commission\Agent as AgentModel;
use addons\shopro\model\commission\Reward;
use addons\shopro\model\User;
use addons\shopro\library\teams\Team as TeamLibrary;
use addons\shopro\library\teams\Config as ConfigLibrary;
use app\admin\model\shopro\teams\Order;

class Team extends Commission
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**
     * 团长详情
     */
    public function info()
    {
        $user = $this->auth->getUserinfo();
        $team = new TeamLibrary($user['id']);
        $teamInfo = $team->getTeamInfo();
        // 是否显示申请信息入口
        $teamInfo['team_form'] = true;
        $teamInfo['upgrade_display'] = true;
        // 未满足成为分销商条件
        // if($agentInfo['status'] == null) {
        $teamInfo['become_agent'] = $this->config->getRetailerNum();
        // }
        if ($team->isTeamAvaliable()) {
            // 待入账佣金
            $teamInfo['data']['delay_money'] = $team->getTeamDelayMoney();
        }
        $TeamOrderMoney = $team->getTeamOrderMoney();
        $teamInfo['data']['heads_money']  = $user['heads_money']; //团获取的佣金
        //团队订单数量、团队订单金额
//        $teamInfo['data']['heads_order_count'] = $TeamOrderMoney['heads_order_count'] ;
//        $teamInfo['data']['heads_order_money'] =  $TeamOrderMoney['heads_order_money'] ;
        //团队完成订单数量、团队完成订单金额
        $orderInfo = Order::field('sum(amount) as amount,count(1) as num')->where(['heads_sid'=>$user['id'],'commission_reward_status'=>'1'])->find();
        $teamInfo['data']['heads_order_count'] =  intval($orderInfo['num']);
        $teamInfo['data']['heads_order_money'] = $orderInfo['amount'] > 0 ? $orderInfo['amount'] : 0 ;
        $this->success('获取成功', $teamInfo);
    }

    /**
     * 我的团队
     *
     * @return void
     */
    public function team()
    {
        $params = $this->request->get();
        extract($params);
        $this->auth->setAllowFields(['id', 'username', 'nickname', 'mobile', 'avatar', 'score', 'money', 'parent_user_id']);
        $user = $this->auth->getUserinfo();
        $teams_user_num = User::where('heads_sid', $user['id'])->count();

        $teams_agent_num = User::where('heads_sid', $user['id'])->alias('u')
            ->join('shopro_commission_agent a','u.id = a.user_id','inner')
            ->count();


        $fields = 'id,nickname,avatar,parent_user_id,child_user_count_1,u.createtime';

        $model = (new User())->where('heads_sid', $user['id'])->alias('u');

        if($params['type'] == 'agent'){
            $model->join('shopro_commission_agent a','u.id = a.user_id','inner');
        }

        $teams = $model->with(['agent' => function ($query) {
            $query->avaliable()->with(['level' => function ($query) {
                $query->field('level,name,image');
            }]);
        }])
            ->field($fields)
            ->order('u.createtime', 'asc')
            ->paginate($per_page ?? 10);


        $this->success('团队中心', [
            'teams_agent_num'=>$teams_agent_num,
            'teams_user_num'=>$teams_user_num,
            'teams' => $teams,
        ]);
    }


}
